package team.scau.laboratory_repair.common.utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class WechatTool {
    private static final String APPID = "wxa682f74b488204a2";
    private static final String APPSECRET = "0eb5bf5727d80e626c0cc801d4e30876";

    public static String getOpenId(String code)  throws Exception {
        String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + APPID + "&secret=" + APPSECRET + "&code=" + code + "&grant_type=authorization_code";

        URL url = new URL(requestUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.connect();
        //获取返回的字符
        InputStream inputStream = connection.getInputStream();

        int size = inputStream.available();
        byte[] bs = new byte[size];
        inputStream.read(bs);
        String message = new String(bs, "UTF-8");
        //获取openid
        JSONObject jsonObject = new JSONObject(message);
        //Code无效错误
        if(jsonObject.has("errcode")){
            throw new Exception();
        }
        return jsonObject.getString("openid");
    }

    public static void postMenu() throws Exception{
        //微信菜单推送地址

        String menuUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
        //获取access_token
        String accessToken = getAccessToken();
        System.out.println(accessToken);
        menuUrl = menuUrl + accessToken;
        URL url = new URL(menuUrl);
        //建立连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.connect();
        //发送菜单字符流
        OutputStream outputStream = connection.getOutputStream();
        outputStream.write(getMenuStr().getBytes("UTF-8"));
        outputStream.flush();
        outputStream.close();
        //接收返回信息
        InputStream inputStream = connection.getInputStream();
        int size = inputStream.available();
        byte[] bs = new byte[size];
        inputStream.read(bs);
        String message = new String(bs, "UTF-8");
        System.out.println(message);
    }

    public static String getMenuStr() throws JSONException, Exception {

        JSONObject menu = new JSONObject();//一级菜单对象
        JSONArray list = new JSONArray();//一级菜单列表
        //一级菜单内容（一级菜单最多允许3）
        JSONObject subButton = new JSONObject();

        //一级菜单内容的二级菜单列表
        JSONArray subList = new JSONArray();

        //一级菜单内容2的二级菜单内容2
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("type", "view");
        jsonObject.put("name", "进入系统");
        jsonObject.put("url", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + APPID +"&redirect_uri=http%3a%2f%2f47.106.110.191%3a8080%2fmobile%2fstart%2fHomePage&response_type=code&scope=snsapi_base&state=123#wechat_redirect");
        subList.put(jsonObject);

        subButton.put("name", "维修系统");
        subButton.put("sub_button", subList);

        list.put(subButton);

        menu.put("button", list);
        return menu.toString();
    }

    public static String getAccessToken() throws Exception {
        //获取accss_token的地址
        String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + APPSECRET;

        URL url = new URL(accessTokenUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.connect();
        //获取返回的字符
        InputStream inputStream = connection.getInputStream();

        int size = inputStream.available();
        byte[] bs = new byte[size];
        inputStream.read(bs);
        String message = new String(bs, "UTF-8");
        //获取access_token

        JSONObject jsonObject = new JSONObject(message);
        return jsonObject.getString("access_token");

    }
}
